using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Tests
{
    [TestClass()]
    public class OrderServiceTests
    {
        [TestMethod()]
        public void ExportTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            service.Export();

            List<Order> orders = new List<Order>();
            XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("s.xml", FileMode.Open))
            {
                List<Order> orderstmp = (List<Order>)serializer.Deserialize(fs);
                orders.AddRange(orderstmp);
            }
            var object1Json = JsonConvert.SerializeObject(service.orders);
            var object2Json = JsonConvert.SerializeObject(orders);

            Assert.AreEqual(object1Json, object2Json);
        }

        [TestMethod()]
        public void ImportTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            service.Export();
            
            List <Order> orders = new List<Order>();
            orders.Add(order);
            var object1Json = JsonConvert.SerializeObject(service.orders);
            var object2Json = JsonConvert.SerializeObject(orders);

            Assert.AreEqual(object1Json, object2Json);
        }

        [TestMethod()]
        public void AddOrderTest()
        {
            OrderService service = new OrderService();
            Order order =new Order(1,12,"www","www");
            service.AddOrder(order);
            Assert.AreEqual(order, service.GetOrderId(1));
        }

        [TestMethod()]
        public void DeleteOrderTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            service.DeleteOrder(1);
            Assert.IsNull(service.GetOrderId(1));
        }

        [TestMethod()]
        public void GetOrderIdTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            Order order1 = service.GetOrderId(1);
            Assert.AreEqual (order1, order);
        }

        [TestMethod()]
        public void GetOrderNameTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            List<Order> order1 = service.GetOrderName("www");
            Assert.IsNotNull(order1);
        }

        [TestMethod()]
        public void GetOrderCustomerTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            List<Order> order1 = service.GetOrderCustomer("www");
            Assert.IsNotNull (order1);
        }

        [TestMethod()]
        public void GetOrderMoneysTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            List<Order> order1 = service.GetOrderMoneys(12);
            Assert.IsNotNull(order1);
        }

        [TestMethod()]
        public void UpdateOrderTest()
        {
            OrderService service = new OrderService();
            Order order = new Order(1, 12, "www", "www");
            service.AddOrder(order);
            service.UpdateOrder(1,"qqq","qqq",22);
            List<Order> orders = service.GetOrderName("qqq");
            Assert.IsNotNull(orders);
            
        }

        [TestMethod()]
        public void sortOrdersTest()
        {
            OrderService service = new OrderService();
            Order[] orders = { new Order(1, 12, "www", "www"), new Order(2, 12, "www", "www") , new Order(3, 12, "www", "www"), new Order(4, 12, "www", "www"), new Order(5, 12, "www", "www") };
            List<Order> orders2 = new List<Order>();
            for(int i = 0; i < 5; i++)
            {
                Order order = new Order(i+1, 12, "www", "www");
                orders2.Add(order);
            }
            for(int i=0; i < orders.Length; i++)
            {
                service.AddOrder(orders[orders.Length-i-1]);
            }
            service.sortOrders();
            //非基础类型的比较，需要重写equal函数，这里将俩对象转化为字符串比较
            var object1Json = JsonConvert.SerializeObject(service.orders);
            var object2Json = JsonConvert.SerializeObject(orders2);

            Assert.AreEqual(object1Json, object2Json);
        }
    }
}